package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.monitor.comm.dto.*;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.dto.DtuHDRequestData;
import com.runa.rccprotocol.dto.ConcentratorRequestDataProxy;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import com.runa.rccprotocol.dto.runa.RunaLoraRequestData;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
public class RunaLoraDriver extends NetEqu<String> {

    public RunaLoraDriver(String serial, Integer timeout) {
        super(serial, timeout);
    }

    @Override
    public void readNodeEqu(List<Node> nodeList, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        switch (type) {
            case METER:
                readHouseMeter(nodeList, function);
                break;
            case PUBLIC_UNIT:
                readBuildValve(nodeList, function);
                break;
            default:
        }
    }

    private void readBuildValve(List<Node> nodes, TaskFunction function) {
        for (Node node : nodes) {
            if (node != null) {
                RunaLoraRequestData requestData = new RunaLoraRequestData(this.serial());
                requestData.readBuildValve(node.getSerial(), node.getPath());
                ConcentratorRequestDataProxy dataProxy = new ConcentratorRequestDataProxy(serial(),
                        new DtuHDRequestData(this.serial(), requestData));
                dataProxy.setRemain(Collections.singletonList(node));
                sendRequest(dataProxy, function);
            }
        }
    }

    private void readHouseMeter(List<Node> nodes, TaskFunction function) {
        for (Node node : nodes) {
            if (node != null) {
                RunaLoraRequestData requestData = new RunaLoraRequestData(this.serial());
                requestData.readHeatMeter(node.getSerial(), node.getPath());
                ConcentratorRequestDataProxy dataProxy = new ConcentratorRequestDataProxy(serial(),
                        new DtuHDRequestData(this.serial(), requestData));
                dataProxy.setRemain(Collections.singletonList(node));
                sendRequest(dataProxy, function);
            }
        }
    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void valveOnOffControl(List<Node> valveSerial, boolean ifLock, Integer opening, TaskFunction function) {

    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> valves, Integer model, Map param, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, valves);
            return;
        }
        for (Node node : valves) {
            if (node != null && model == 1) {
                RunaLoraRequestData requestData = new RunaLoraRequestData(this.serial());
                requestData.setBuildValveOpen(node.getSerial(), (int) Float.parseFloat(param.get("targetValue").toString()), node.getPath());
                ConcentratorRequestDataProxy dataProxy = new ConcentratorRequestDataProxy(serial(),
                        new DtuHDRequestData(this.serial(), requestData));
                dataProxy.setRemain(node);
                sendRequest(dataProxy, function);
            }
        }
    }

    @Override
    public void sentPublicUnitModel(List<Node> valveSerial, boolean model, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, valveSerial);
            return;
        }
        for (Node node : valveSerial) {
//            Node node = findBuildValve(valve);
            if (node != null) {
                RunaLoraRequestData requestData = new RunaLoraRequestData(serial());
                requestData.setBuildValveModel(node.getSerial(), model, node.getPath());
                ConcentratorRequestDataProxy dataProxy = new ConcentratorRequestDataProxy(serial(),
                        new DtuHDRequestData(this.serial(), requestData));
                dataProxy.setRemain(node);
                sendRequest(dataProxy, function);
            }
        }
    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valveSerial, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valveSerial, Node meterSerial, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }

//    public Node findMeter(String meter) {
//        for (Node node : meters) {
//            if (meter.equals(node.getSerial())) {
//                return node;
//            }
//        }
//        return null;
//    }

    public Node findValve(String serial,String netId) {
        return null;
    }


//    @Override
//    public Node findUnit(String serial) {
//        for (Node node : buildValves) {
//            if (serial.equals(node.getSerial())) {
//                return node;
//            }
//        }
//        return null;
//    }
//
//    private Node findBuildValve(String valve) {
//        for (Node node : buildValves) {
//            if (valve.equals(node.getSerial())) {
//                return node;
//            }
//        }
//        return null;
//    }

}
